package com.xialuo.shiro;


import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Component
@Configuration
public class ShiroRedisConfig {

    @Autowired
    JwtFilter jwtFilter;
    @Bean
    public SessionManager sessionManager(RedisSessionDAO redisSessionDAO) {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();

        // inject redisSessionDAO
        sessionManager.setSessionDAO(redisSessionDAO);
        return sessionManager;
    }

    @Bean
    public SessionsSecurityManager securityManager(@Qualifier("userRealm")UserRealm userRealm, SessionManager sessionManager
            ,RedisSessionDAO redisSessionDAO
            ,RedisCacheManager redisCacheManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(userRealm);

        //inject sessionManager
        securityManager.setSessionManager(sessionManager);

        // inject redisCacheManager
        securityManager.setCacheManager(redisCacheManager);
        return securityManager;
    }

    @Bean("userRealm")
    public UserRealm getRealm(){

        return new UserRealm();
    }
    /**
     * 创建ShiroFilterFactoryBean
     *
     */
    @Bean
    public ShiroFilterChainDefinition shirFilter(DefaultWebSecurityManager securityManager) {
        DefaultShiroFilterChainDefinition shiroFilterFactoryBean = new DefaultShiroFilterChainDefinition();

        System.out.println("jinru qweqweqwwwwwwwwwwwwww");
        // 设置无权限时跳转的 url;
        // 设置拦截器
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
       //游客，开发权限
        filterChainDefinitionMap.put("/toLogin", "anon");
        filterChainDefinitionMap.put("/notRole", "anon");
        filterChainDefinitionMap.put("/index", "anon");
        filterChainDefinitionMap.put("/**", "authc");

        shiroFilterFactoryBean.addPathDefinitions(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }

    /**
     * 注入 securityManager
     */
    @Bean("securityManager")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager
                                                         ,ShiroFilterChainDefinition ShiroFilterChainDefinition) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, Filter> filters =new HashMap<>();
        filters.put("jwt",jwtFilter);
        shiroFilterFactoryBean.setFilters(filters);

        Map<String, String> filterChainMap = ShiroFilterChainDefinition.getFilterChainMap();
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainMap);

        return shiroFilterFactoryBean;
    }

}
